﻿using System;
using System.Collections.Generic;
using DataEntities;
using NUnit.Framework;
using Repositories.Repository;
using Repositories.UoW;

namespace TestRepositories
{
    [TestFixture]
    public class TestDogovorRepository:ITestRepository
    {
        private IDogovorRepository _repository;
        private IDistrictRepository _districtRepository;
        private ISectorRepository _sectorRepository;
        private RepositoryFactory _repositoryFactory;
        private Dogovor _dogovor;

        [SetUp]
        public void SetUp()
        {
            SessionFactory.Init();
            SessionFactory.GetSession().Flush();
            _repositoryFactory = new RepositoryFactory();
            _repository = _repositoryFactory.GetDogovorRepository();
            _districtRepository = _repositoryFactory.GetDistrictRepository();
            _sectorRepository = _repositoryFactory.GetSectorRepository();


            _dogovor = new Dogovor(1000);
            _dogovor.AbonentPay = 1000;
            _dogovor.District = _districtRepository.Get(5);
            _dogovor.Sector = _sectorRepository.Get(1);
            _dogovor.DogovorCreateDate = new DateTime(2011, 08, 03);
            _dogovor.Entrance = "1000";
            _dogovor.ExceptFromCitySystem = false;
            _dogovor.Home = "1000";
            _dogovor.IsAbonentPayAutoChanged = false;
            _dogovor.Street = "aaaaaaaaaaaaaaaaaaa";
        }

        [TearDown]
        public void CleanUp()
        {
        }

        [Test]
        public void TestGet()
        {
            Assert.AreEqual(8, _repository.Get(8).Id);
        }

        [Test]
        public void TestGetList()
        {
            Assert.AreEqual(482, _repository.GetList().Count);
        }

        [Test]
        public void TestInsert()
        {

            Assert.AreEqual(1, _repository.Insert(_dogovor));
        }

        [Test]
        public void TestUpdate()
        {
            _dogovor = _repository.Get(3);
            _dogovor.Street = "TestUpdated";
            Assert.AreEqual(1, _repository.Update(_dogovor));
        }

        [Test]
        [Ignore]
        public void TestDelete()
        {
            _dogovor = _repository.Get(3);
            Assert.AreEqual(1, _repository.Delete(_dogovor));
        }

        [Test]
        public void TestEquals()
        {
            _dogovor = new Dogovor(3);
            _dogovor.District = _districtRepository.Get(2);
            _dogovor.Sector = _sectorRepository.Get(33);
            _dogovor.AbonentPay = 30;
            _dogovor.Street = "Набережная Волжской Флотилии";
            _dogovor.Home = "1";
            _dogovor.Entrance = "2";
            _dogovor.IsAbonentPayAutoChanged = false;
            _dogovor.ExceptFromCitySystem = false;

           Assert.AreEqual(_dogovor, _repository.Get(3));
        }

        [Test]
        public void TestInsertList()
        {
            Dogovor _dogovor1 = new Dogovor(1001);
            _dogovor1.AbonentPay = 1001;
            _dogovor1.District = _districtRepository.Get(5);
            _dogovor1.Sector = _sectorRepository.Get(1);
            _dogovor1.DogovorCreateDate = new DateTime(2011, 08, 03);
            _dogovor1.Entrance = "1000";
            _dogovor1.ExceptFromCitySystem = false;
            _dogovor1.Home = "1000";
            _dogovor1.IsAbonentPayAutoChanged = false;
            _dogovor1.Street = "aaaaaaaaaaaaaaaaaaa";

            Dogovor _dogovor2 = new Dogovor(1002);
            _dogovor2.AbonentPay = 1002;
            _dogovor2.District = _districtRepository.Get(5);
            _dogovor2.Sector = _sectorRepository.Get(1);
            _dogovor2.DogovorCreateDate = new DateTime(2011, 08, 03);
            _dogovor2.Entrance = "1000";
            _dogovor2.ExceptFromCitySystem = false;
            _dogovor2.Home = "1000";
            _dogovor2.IsAbonentPayAutoChanged = false;
            _dogovor2.Street = "aaaaaaaaaaaaaaaaaaa";

            IList<Dogovor> list = new List<Dogovor>();
            list.Add(_dogovor1);
            list.Add(_dogovor2);

            Assert.AreEqual(2, _repository.InsertList(list));
        }

        [Test]
        public void UpdateList()
        {
            IList<Dogovor> list = new List<Dogovor>();
            list.Add(_repository.Get(16));
            list.Add(_repository.Get(5));
            list.Add(_repository.Get(6));
            list.Add(_repository.Get(10));

            foreach (Dogovor item in list)
            {
                item.Street += " UpdatedList";
            }

            Assert.AreEqual(4, _repository.UpdateList(list));
        }

        [Test]
        [Ignore]
        public void TestDeleteList()
        {
            IList<Dogovor> list = new List<Dogovor>();
            list.Add(_repository.Get(3));
            list.Add(_repository.Get(5));
            list.Add(_repository.Get(6));
            list.Add(_repository.Get(7));
            list.Add(_repository.Get(8));
            list.Add(_repository.Get(9));

            Assert.AreEqual(6, _repository.DeleteList(list));
        }

        [Test]
        
        public void TestGetChildHistoryAbonentPaysList()
        {
            Assert.AreEqual(5, _repository.Get(3).HistoryAbonentPayList.Count);
        }

        [Test]
        public void TestGetChildDogovorExtraInfoList()
        {
            Assert.AreEqual(13, _repository.Get(3).DogovorExtraInfoList.Count);
        }

        [Test]
        public void TestGetChildHistoryCommonReceiptsDataList()
        {
            Assert.AreEqual(23, _repository.Get(10).HistoryCommonReceiptsDataList.Count);
        }

        [Test]
        public void TestGetChildPeopleList()
        {
            Assert.AreEqual(38, _repository.Get(12).PeopleList.Count);
        }
    }
}